package com.demoworld;

import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.api.strategy.impl.AverageAllocationJobShardingStrategy;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;

/**
 * Description
 * <p>
 * </p>
 * DATE 2018/9/14.
 *
 * @author liwei5.
 */
public class Startup {
    public static void main(String[] args) {
        new JobScheduler(registryCenter(), configuration()).init();
    }

    //注册job
    private static CoordinatorRegistryCenter registryCenter() {
        String serverLists = "127.0.0.1:2181";
        String namespace = "elastic-job";
        CoordinatorRegistryCenter registryCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverLists, namespace));
        registryCenter.init();
        return registryCenter;
    }

    //添加job 配置
    private static LiteJobConfiguration configuration() {
        String jobName = "simpleJob";
        String cron = "0/15 * * * * ?";
        int shardingTotalCount = 3;
        JobCoreConfiguration coreConfiguration = JobCoreConfiguration.newBuilder(jobName, cron, shardingTotalCount).build();
        String jobClass = MySimpleJob.class.getCanonicalName();
        SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(coreConfiguration, jobClass);

        LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration
                .newBuilder(simpleJobConfiguration)
                .jobShardingStrategyClass(AverageAllocationJobShardingStrategy.class.getCanonicalName())
                .build();
        return simpleJobRootConfig;
    }
}
